package com.etb.wisdom.eframe.org.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.etb.core.pojo.CriteriaType;
import com.etb.core.pojo.DataObject;
import com.etb.core.service.ETBService;
import com.etb.core.utils.DataUtil;
import com.etb.core.utils.SequenceUtil;
import com.etb.web.eui.tree.TreeNode;
import com.etb.web.eui.tree.TreeNode.State;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.dict.DictConstant;
import com.etb.wisdom.eframe.model.EtbPartyAuth;
import com.etb.wisdom.eframe.model.OrgEmployee;
import com.etb.wisdom.eframe.model.OrgOrganization;
import com.etb.wisdom.eframe.model.OrgPosition;
import com.etb.wisdom.eframe.org.service.OrgOrganizationService;

@Service
public class OrgOrganizationServiceImpl extends ETBService implements OrgOrganizationService {
	
	@Override
	public List<OrgOrganization> query(DataObject dataObject, String... args) {
		//TODO 添加数据权限
		return service.query(dataObject, args);
	}
	
	@Override
	public List queryAll(String... args) {
		//TODO 添加数据权限
		return service.queryAll(OrgOrganization.class, args);
	}

	@Override
	public List<TreeNode> getTreenode(String id, String nodeType) {
		List<TreeNode> treedata = new ArrayList<TreeNode>();
		if(StringUtils.isBlank(id)) {
			TreeNode treenode = new TreeNode(TREENODEID_ROOT, TREENODENAME_ROOT);
			treenode.put("nodeType", TREENODETYPE_ROOT);
			treenode.setState(State.closed.toString());
			treenode.setIconCls("icon-root");
			treedata.add(treenode);
		} else if(id.equals(TREENODEID_ROOT)) {
			OrgOrganization orgTemplate = new OrgOrganization();
			orgTemplate.setOrglevel("1");
			List<OrgOrganization> orgs = this.query(orgTemplate);
			for(OrgOrganization org: orgs) {
				TreeNode treenode = new TreeNode(org.getId(), org.getOrgname());
				treenode.put("nodeType", TREENODETYPE_ORGANIZATION);
				treenode.put("orglevel", org.getOrglevel());
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-org");
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_ORGANIZATION.equals(nodeType)) {
			OrgOrganization orgTemplate = new OrgOrganization();
			orgTemplate.setUplevelid(id);
			List<OrgOrganization> orgs = this.query(orgTemplate);
			for(OrgOrganization org: orgs) {
				TreeNode treenode = new TreeNode(org.getId(), org.getOrgname());
				treenode.put("nodeType", TREENODETYPE_ORGANIZATION);
				treenode.put("orglevel", org.getOrglevel());
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-org");
				treedata.add(treenode);
			}
			OrgPosition posiTemplate = new OrgPosition();
			posiTemplate.setOrgid(id);
			posiTemplate.setPosilevel("1");
			List<OrgPosition> positions = service.query(posiTemplate);
			for(OrgPosition position: positions) {
				TreeNode treenode = new TreeNode(position.getId(), position.getPosiname());
				treenode.put("nodeType", TREENODETYPE_POSITION);
				treenode.put("posilevel", position.getPosilevel());
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-position");
				treedata.add(treenode);
			}
			CriteriaType criteria = new CriteriaType();
			criteria.setEntity(OrgEmployee.class.getName());
			criteria.addExpr("orgid_eq", id);
			criteria.addExpr("positionid_eq", null);
			List<OrgEmployee> emps = service.query(criteria);
			for(OrgEmployee emp: emps) {
				TreeNode treenode = new TreeNode(emp.getId(), emp.getEmpname());
				treenode.put("nodeType", TREENODETYPE_EMPLOYEE);
				treenode.setIconCls(DictConstant.GENDER_GIRL.equals(emp.getGender()) ? "icon-girl" : "icon-boy");
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_POSITION.equals(nodeType)) {
			OrgPosition posiTemplate = new OrgPosition();
			posiTemplate.setUplevelid(id);
			List<OrgPosition> positions = service.query(posiTemplate);
			for(OrgPosition position: positions) {
				TreeNode treenode = new TreeNode(position.getId(), position.getPosiname());
				treenode.put("nodeType", TREENODETYPE_POSITION);
				treenode.put("posilevel", position.getPosilevel());
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-position");
				treedata.add(treenode);
			}
			OrgEmployee empTemplate = new OrgEmployee();
			empTemplate.setPositionid(id);
			List<OrgEmployee> emps = service.query(empTemplate);
			for(OrgEmployee emp: emps) {
				TreeNode treenode = new TreeNode(emp.getId(), emp.getEmpname());
				treenode.put("nodeType", TREENODETYPE_EMPLOYEE);
				treenode.setIconCls(DictConstant.GENDER_GIRL.equals(emp.getGender()) ? "icon-girl" : "icon-boy");
				treedata.add(treenode);
			}
		}
		return treedata;
	}

	public void save(OrgOrganization org) {
		service.save(org);
		if(org.getUplevelid() == null) {
			org.setOrgseq(org.getId());
		} else {
			OrgOrganization uplevelOrg = new OrgOrganization();
			uplevelOrg.setId(org.getUplevelid());
			service.extend(uplevelOrg);
			org.setOrgseq(SequenceUtil.generateNewSequence(uplevelOrg.getOrgseq(), org.getId()));
		}
		service.save(org);
	}

	@Override
	public void saveRole(String orgId, String addRoleIds, String delRoleIds) {
		List<String> addIdList = DataUtil.split(addRoleIds);
		List<String> delIdList = DataUtil.split(delRoleIds);
		for(String id: addIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(orgId);
			auth.setPartytype(PartyType.organization.toString());
			auth.setAuthid(id);
			auth.setAuthtype(AuthType.role.toString());
			service.save(auth);
		}
		for(String id: delIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(orgId);
			auth.setPartytype(PartyType.organization.toString());
			auth.setAuthid(id);
			auth.setAuthtype(AuthType.role.toString());
			service.delete(auth);
		}
	}
	
}
